package com.xwyzsn.backend.job.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwyzsn.backend.job.common.Result;
import com.xwyzsn.backend.job.entity.Teacher;
import com.xwyzsn.backend.job.mapper.ProjectMapper;
import com.xwyzsn.backend.job.mapper.ProjectStudentUnion;
import com.xwyzsn.backend.job.mapper.StudentMapper;
import com.xwyzsn.backend.job.mapper.TeacherMapper;
import com.xwyzsn.backend.job.service.TeacherService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xwyzsn
 * @since 2023-03-03
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {


    @Autowired
    private TeacherMapper teacherMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectStudentUnion projectStudentUnionMapper;
    @Autowired
    private StudentMapper studentMapper;

    public Result getInfo(String tnum) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tnum", tnum);
        Teacher teacher = teacherMapper.selectOne(queryWrapper);
        if (teacher == null) {
            return Result.fail("user not exist");
        }
        return Result.success(teacher);
    }

    public Result getProject(String tnum) {
        QueryWrapper<ProjectStudentUnion> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tnum", tnum);
//        List<ProjectStuDetail> l = projectStudentUnionMapper.selectList(queryWrapper);
//        if(l.isEmpty()){
        List<ProjectStudentUnion> l = projectStudentUnionMapper.selectUnionByTid(queryWrapper);
        System.out.println(l);
//            return Result.success(0);
//        }
//        List<Project> project = projectMapper.selectList(queryWrapper);
//        List<Object>l = new ArrayList<>();
//        for(Project p:project){
//            if(p.getSnum()!=null){
//                QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
//                studentQueryWrapper.eq("snum",p.getSnum());
//                Student student = studentMapper.selectOne(studentQueryWrapper);
//                ProjectStuDetail projectStuDetail = new ProjectStuDetail(p,student);
//                l.add(projectStuDetail);
//            }else{
//                ProjectStuDetail projectStuDetail = new ProjectStuDetail(p,null);
//                l.add(projectStuDetail);
//            }
//        }
//        if(l.isEmpty()){
//            return Result.success(0);
//        }
        return Result.success(l);
    }

    public Result updateTeacher(String tnum, String tname, Integer age, String tqq, String tmail, String tphone, String tdept, String ttitle, String location, String password) {
        UpdateWrapper<Teacher> teacherUpdateWrapper = new UpdateWrapper<>();
        teacherUpdateWrapper.eq("tnum", tnum);
        Teacher teacher = new Teacher();
        teacher.setTname(tname);
        teacher.setAge(age);
        teacher.setQq(tqq);
        teacher.setEmail(tmail);
        teacher.setTel(tphone);
        teacher.setDept(tdept);
        teacher.setTtile(ttitle);
        teacher.setLocation(location);
        teacher.setPassword(password);
        int i = teacherMapper.update(teacher, teacherUpdateWrapper);
        if (i == 0) {
            return Result.fail("update fail");
        }
        return Result.success("update success");
    }

    public Result getDeptTeacher(String dept) {
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("dept", dept);
        List<Teacher> teacher = teacherMapper.selectList(queryWrapper);
        if (teacher.isEmpty()) {
            return Result.fail("no teacher");
        }
        return Result.success(teacher);
    }
}
